/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.building;

import cz.fi.muni.diplomka.common.Coordinates;
import cz.fi.muni.diplomka.common.enumeration.BuildingType;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.servicelayer.dto.BuildingInCity;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityBuildingManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityBuildingManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.xmlconvertors.BuildingConvertor;
import cz.muni.fi.diplomka.servicelayer.xmlconvertors.LimitsConvertor;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

/**
 *
 * @author Jakub Kutil
 */
public class BuildPanel extends Panel {

    private CityBuildingManager cityBuildingManager = new CityBuildingManagerImpl();
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private Long cityId = new Long(0);
    private int x = 0;
    private int y = 0;

    public BuildPanel(String id, final PageParameters parameters) {
        super(id);

        if (parameters.containsKey("cityId")) {
            cityId = Long.parseLong(parameters.getString("cityId"));
        }

        if (parameters.containsKey("x")) {
            x = Integer.parseInt(parameters.getString("x"));
        }

        if (parameters.containsKey("y")) {
            y = Integer.parseInt(parameters.getString("y"));
        }

        List<BuildingInCity> list = new ArrayList<>();

        LimitsConvertor limitsConvertor = new LimitsConvertor();

        final BuildingConvertor buildingConvertor = new BuildingConvertor();
        for (BuildingType buildingType : BuildingType.values()) {

            if (limitsConvertor.getBuildingLimitsFromXML(buildingType.toString())
                    > cityBuildingManager.getAllBuildingsInCityOfType(cityId, buildingType).size()) {
                list.add(buildingConvertor.getBuildingFromXML(buildingType.toString()));
            }
        }


        ListView listview = new ListView("listview", list) {
            @Override
            protected void populateItem(ListItem item) {
                final BuildingInCity buildingInCity = (BuildingInCity) item.getModelObject();
                item.add(new Label("name", buildingInCity.getName().toString()));
                item.add(new Label("food", buildingInCity.getLevels().get(1).getPriceFood() + ""));
                item.add(new Label("gold", buildingInCity.getLevels().get(1).getPriceGold() + ""));
                item.add(new Label("stone", buildingInCity.getLevels().get(1).getPriceStone() + ""));
                item.add(new Label("time", buildingInCity.getLevels().get(1).getPriceTime() + ""));


                String requires = "";
                for (BuildingType key : buildingInCity.getLevels().get(1).getRequires().keySet()) {
                    requires += key + " level: " + buildingInCity.getLevels().get(1).getRequires().get(key).toString() + " ";
                }
                item.add(new Label("requires", requires));

                Form<?> build = new Form<Void>("build") {
                    @Override
                    protected void onSubmit() {

                        boolean ok = true;
                        boolean requirements = false;
                        if (buildingInCity.getLevels().get(1).getPriceFood() > cityResourcesManager.getResourceCount(cityId, ResourceType.Food)) {
                            ok = false;
                            error("You do not have enough food to build.");
                        }
                        if (buildingInCity.getLevels().get(1).getPriceGold() > cityResourcesManager.getResourceCount(cityId, ResourceType.Gold)) {
                            ok = false;
                            error("You do not have enough gold to build.");
                        }
                        if (buildingInCity.getLevels().get(1).getPriceStone() > cityResourcesManager.getResourceCount(cityId, ResourceType.Stone)) {
                            ok = false;
                            error("You do not have enough stone to build.");
                        }
                        for (BuildingType buildingType : buildingInCity.getLevels().get(1).getRequires().keySet()) {
                            for (BuildingInCity building : cityBuildingManager.getAllBuildingsInCityOfType(cityId, buildingType)) {
                                if (building.getLevel() >= buildingInCity.getLevels().get(1).getRequires().get(buildingType)) {
                                    requirements = true;
                                    break;
                                }
                            }
                        }
                        if (ok == true && requirements == true) {
                            BuildingInCity toBuild = buildingConvertor.getBuildingFromXML(buildingInCity.getName().toString());
                            toBuild.setLevel(1);

                            Coordinates c = new Coordinates();
                            c.setX_coordinate(x);
                            c.setY_coordinate(y);
                            cityBuildingManager.buildBuilding(cityId, toBuild, c);
                            cityResourcesManager.lowerResource(cityId, ResourceType.Food, toBuild.getLevels().get(1).getPriceFood());
                            cityResourcesManager.lowerResource(cityId, ResourceType.Gold, toBuild.getLevels().get(1).getPriceGold());
                            cityResourcesManager.lowerResource(cityId, ResourceType.Stone, toBuild.getLevels().get(1).getPriceStone());

                            setResponsePage(BuildingPage.class, parameters);
                        }
                        if (requirements != true) {
                            error("Requirements to build are not met.");
                        }

                    }
                };

                item.add(build);
            }
        };
        add(listview);
    }
}